﻿
    using System;
    using System.Text;
namespace WCYCReader.Utility
{
    public class Gb2312Encoding : Encoding
    {
        public override int GetByteCount(char[] chars, int index, int count)
        {
            return count;
        }

        public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
        {
            throw new NotImplementedException();
        }

        public override int GetCharCount(byte[] bytes, int index, int count)
        {
            return count;
        }

        public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
        {
            int index = 0;
            for (int i = 0; i < byteCount; i += 2)
            {
                if ((i + 1) >= bytes.Length)
                {
                    char[] chArray = Encoding.UTF8.GetChars(new byte[] { bytes[i] });
                    chars[index] = chArray[0];
                }
                else
                {
                    char ch;
                    byte[] b = new byte[] { bytes[i], bytes[i + 1] };
                    if (Gb2312toUnicode.TryGetChar(b, out ch))
                    {
                        chars[index] = ch;
                        index++;
                    }
                    else
                    {
                        char[] chArray2 = Encoding.UTF8.GetChars(new byte[] { b[1] });
                        chars[index] = chArray2[0];
                        index++;
                        if ((i + 2) >= bytes.Length)
                        {
                            char[] chArray3 = Encoding.UTF8.GetChars(new byte[] { b[0] });
                            chars[index] = chArray3[0];
                            index++;
                        }
                        else
                        {
                            byte[] buffer2 = new byte[] { b[0], bytes[i + 2] };
                            if (Gb2312toUnicode.TryGetChar(buffer2, out ch))
                            {
                                chars[index] = ch;
                                index++;
                                i++;
                            }
                            else
                            {
                                char[] chArray4 = Encoding.UTF8.GetChars(new byte[] { b[0] });
                                chars[index] = chArray4[0];
                                index++;
                            }
                        }
                    }
                }
            }
            return chars.Length;
        }

        public override int GetMaxByteCount(int charCount)
        {
            return charCount;
        }

        public override int GetMaxCharCount(int byteCount)
        {
            return byteCount;
        }

        public static int CharacterCount
        {
            get
            {
                return 0x1d02;
            }
        }

        public override string WebName
        {
            get
            {
                return "gb2312";
            }
        }
    }
}

